package mysql

import (
	"gorm.io/gorm"
	"video-hubs/videorpc/videorpc"
)

type User struct {
	gorm.Model
	Username string `gorm:"type:varchar(20);not null"`
	Password string `gorm:"type:char(32)"`
	Image    string `gorm:"type:varchar(500);not null"`
	Title    string `gorm:"type:varchar(100);not null"`
}
type Sang struct {
	gorm.Model
	Name   string  `gorm:"type:varchar(30);not null"`
	Type   string  `gorm:"type:varchar(30);not null"`
	Price  float32 `gorm:"type:decimal(10,2);not null"`
	UserId int     `gorm:"type:int(11);not null"`
}
type Shou struct {
	gorm.Model
	UserId int `gorm:"type:int(11);not null"`
	SangId int `gorm:"type:int(11);not null"`
}
type Good struct {
	gorm.Model
	UserId  int    `gorm:"type:int(11);not null"`
	SangId  int    `gorm:"type:int(11);not null"`
	Content string `gorm:"type:varchar(50);not null"`
}

func (g *Good) GoodAdd(db *gorm.DB) error {
	return db.Debug().Create(&g).Error
}

func (g *Good) GoodList(db *gorm.DB, listss []*videorpc.GetList2, in *videorpc.GoodListRequest) ([]*videorpc.GetList2, error) {
	err1 := db.Debug().Model(&g).Table("goods").
		Select(
			"users.image as image",
			"users.title as title",
			"sangs.`name` as name",
			"goods.content as content",
			"sangs.created_at as createdAt",
		).
		Joins("LEFT JOIN sangs ON sangs.id = goods.sang_id").
		Joins("LEFT JOIN users ON users.id = goods.user_id").
		Where("sangs.id = ?", in.SangId).
		Order("sangs.created_at desc").
		Find(&listss).Error
	return listss, err1
}

func (s *Sang) FindSang(db *gorm.DB, name string) error {
	return db.Debug().Where("name = ？", name).First(s).Error
}

func (s *Sang) AddSang(db *gorm.DB) error {
	return db.Debug().Create(&s).Error
}

func (s *Sang) ListSang(db *gorm.DB, list []*videorpc.GetList, in *videorpc.SangListRequest) []*videorpc.GetList {
	tx := db.Debug().Model(&s).Table("sangs").
		Select(
			"sangs.id AS sangID",
			"sangs.`name`as name",
			"sangs.type as type",
			"sangs.price as price",
			"users.image as Image",
			"users.title as Title",
			"sangs.created_at",
		).
		Joins("LEFT JOIN users ON users.id = sangs.user_id").
		Scopes(Paginate(int(in.Page), int(in.Size))).
		Order("created_at desc")

	if in.Type != "" {
		tx = tx.Where("sangs.type = ?", in.Type)
	}
	if in.Name != "" {
		tx = tx.Where("sangs.name = ?", in.Name)
	}
	tx.Find(&list)
	return list
}

func (s *Shou) ShouAdd(db *gorm.DB) error {
	return db.Debug().Create(&s).Error
}

func (s *Shou) ShouList(db *gorm.DB, lists []*videorpc.GetList1, in *videorpc.ShouListRequest) ([]*videorpc.GetList1, error) {
	err2 := db.Debug().Model(&s).Table("shous").
		Select(
			"users.image as image",
			"users.title as title",
			"sangs.`name` as name",
			"sangs.price as price",
			"sangs.type as type",
			"shous.id as shouId",
			"users.id as userId",
		).
		Joins("LEFT JOIN sangs ON sangs.id = shous.sang_id").
		Joins("LEFT JOIN users ON shous.user_id = users.id").
		Where("users.id = ?", in.UserId).Find(&lists).Error
	return lists, err2
}

func (s *Shou) ShouDel(db *gorm.DB, id int64) error {
	return db.Debug().Where("id = ?", id).Delete(s).Error
}

func (u *User) FindUser(db *gorm.DB, username string) error {
	return db.Debug().Where("username = ?", username).First(u).Error
}

func (u *User) AddUsers(db *gorm.DB) error {
	return db.Debug().Create(&u).Error
}

func (u *User) UpdateUser(db *gorm.DB, username string) error {
	return db.Debug().Where("username = ?", username).Updates(u).Error
}
